package utils
object IpUtil {
  def ipToLong(ip: String): Long = {
    //分定循,toLong插棍左八圈
    val ipSplit = ip.split("[.]")
    var ipLong = 0L
    for (ele <- ipSplit) {
      ipLong = ele.toLong | ipLong << 8L
    }
    ipLong
  }


  def searchIpRange(ipLong: Long, bsAry: Array[(String, String, String)]): String = {
    var height = bsAry.length - 1
    var low = 0
    while (low <= height) {
      var mid = (height + low) / 2
      if (ipLong >= bsAry(mid)._1.toLong && ipLong <= bsAry(mid)._2.toLong) {
        //返回一元元组  (亚洲-中国-福建-福州-电信)
        return bsAry(mid)._3
      } else if (ipLong > bsAry(mid)._2.toLong) {
        low = mid + 1
      } else if (ipLong < bsAry(mid)._1.toLong) {
        height = mid - 1
      }
    }
    null
  }
}
